//
//  ThirdLoginMainView.m
//  ProjectForCustomer
//
//  Created by 钟游 on 2019/8/25.
//  Copyright © 2019年 钟游. All rights reserved.
//

#import "ThirdLoginMainView.h"
#import "CodeInputView.h"
#import "LoginViewModel.h"
#import "KLCodeResignView.h"

@interface ThirdLoginMainView()
@property(nonatomic,strong)UIImageView * bgView;
@property(nonatomic,strong)UITextField * phoneNumberTF;
@property(nonatomic,strong)UIView * contentView;
@property(nonatomic,strong)CustomLabel * contentTitleLabel;
@property(nonatomic,strong)UIView * lineView;
@property(nonatomic,strong)CustomButton * loginBtn;
@property(nonatomic,strong)CustomLabel * titleLabel;
@property(nonatomic,strong)CodeInputView * codeView;
@property(nonatomic,strong)LoginViewModel * viewModel;
@property(nonatomic,strong)CustomLabel * codeLabel;
@property(nonatomic,copy)NSString * verificationCode;
@property(nonatomic,strong)NSDictionary * infoDict;
@end
@implementation ThirdLoginMainView
-(instancetype)initWithViewModel:(id<ViewModelProtocol>)viewModel{
    self.viewModel =(LoginViewModel*)viewModel;
    return  [super initWithViewModel:viewModel];
}
-(void)zy_bindViewModel{
    @weakify(self);
    [[self.viewModel.senderCodeSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self updateUI];
    }];
    [[self.viewModel.qqSubject takeUntil:self.rac_willDeallocSignal]subscribeNext:^(id  _Nullable x) {
          @strongify(self);
        self.infoDict = x;
    }];
}
#pragma mark -- 更改UI
-(void)updateUI{
    self.phoneNumberTF.hidden = YES;
    self.codeLabel.hidden = NO;
    KLCodeResignView *codeView = [[KLCodeResignView alloc] initWithCodeBits:6];
    codeView.codeResignUnCompleted = ^(NSString * _Nonnull content) {
        //对应位数输入完成时 允许提交按钮有效 允许提交
        NSLog(@"%@", content);
  
    };
    codeView.codeResignUnCompleted = ^(NSString * _Nonnull content) {
        //对应位数未输入完成时 提交按钮失效 不允许提交
        if (content.length==6) {
            self->_verificationCode = content;
        }
       
    };
    [self addSubview:codeView];
    
    codeView.frame = CGRectMake(70, CGRectGetMaxY(self.contentTitleLabel.frame)+30, KscreenWidth-140, 40);
    self.contentTitleLabel.text = @"验证码";
    [self.loginBtn setTitle:@"下一步" forState:UIControlStateNormal];
}
-(void)zy_setupViews{
    [self addSubview:self.bgView];
    
    [self addSubview:self.contentView];
    [self addSubview:self.contentTitleLabel];
    [self addSubview:self.codeLabel];
    self.codeLabel.hidden = YES;
    [self addSubview:self.phoneNumberTF];
    [self addSubview:self.lineView];
    [self addSubview:self.loginBtn];
   
}
/**
 验证码手机号
 
 @param mobileNum 手机号
 @return YES 通过 NO 不通过
 */
- (BOOL)validateContactNumber:(NSString *)mobileNum
{
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 16[6], 17[5, 6, 7, 8], 18[0-9], 170[0-9], 19[89]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705,198
     * 联通号段: 130,131,132,155,156,185,186,145,175,176,1709,166
     * 电信号段: 133,153,180,181,189,177,1700,199
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|6[6]|7[05-8]|8[0-9]|9[89])\\d{8}$";
    
    NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478]|9[8])\\d{8}$)|(^1705\\d{7}$)";
    
    NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|66|7[56]|8[56])\\d{8}$)|(^1709\\d{7}$)";
    
    NSString *CT = @"(^1(33|53|77|8[019]|99)\\d{8}$)|(^1700\\d{7}$)";
    
    /**
     * 大陆地区固话及小灵通
     * 区号：010,020,021,022,023,024,025,027,028,029
     * 号码：七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    // NSPredicate *regextestPHS = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
    
    if(([regextestmobile evaluateWithObject:mobileNum] == YES)
       || ([regextestcm evaluateWithObject:mobileNum] == YES)
       || ([regextestct evaluateWithObject:mobileNum] == YES)
       || ([regextestcu evaluateWithObject:mobileNum] == YES)) {
        return YES;
    } else {
        return NO;
    }
}
#pragma mark -- clickBtn
-(void)clickBtn{
    if ([self.loginBtn.titleLabel.text isEqualToString:@"获取验证码"]) {
        if ([self validateContactNumber:self.phoneNumberTF.text]) {
            [self.viewModel.senderCodeCommand execute:self.phoneNumberTF.text];
        }else{
            [self makeToast:@"请输入正确手机号" duration:2.0f position:CSToastPositionCenter];
        }
    }else{
        if (self.verificationCode.length==6) {
            NSDictionary * info = @{@"phone":self.phoneNumberTF.text,
                                    @"verificationCode":self.verificationCode,
                                    @"inviteUserId":@""
                                    };
             [self.viewModel.bindPhoneCommand execute:info];
           
        }else{
            [self makeToast:@"请输入正确验证码" duration:2.0f position:CSToastPositionCenter];
        }
    
    }
   
}
#pragma mark -- lazy
-(UIImageView *)bgView{
    if (!_bgView) {
        _bgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, HEIGHT_NAVBAR, KscreenWidth, KscreenHeight-HEIGHT_NAVBAR)];
        _bgView.image=[UIImage imageNamed:@"bg_third"];
    }
    return _bgView;
}
-(UIView *)contentView{
    if (!_contentView) {
        _contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 150, KscreenWidth, CGRectGetHeight(self.bgView.frame)-150)];
        _contentView.layer.cornerRadius = 20;
        _contentView.layer.masksToBounds = YES;
        _contentView.backgroundColor=UIColorFromRGB(0xFFFFFF);
    }
    return _contentView;
}
-(CustomLabel *)contentTitleLabel{
    if (!_contentTitleLabel) {
        _contentTitleLabel=[[CustomLabel alloc]initWithFrame:CGRectMake(50, CGRectGetMinY(self.contentView.frame)+20, 200, 31) andText:@"手机号" andFont:22 andColor:UIColorFromRGB(0x333333)];
        _contentTitleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:22];
    }
    return _contentTitleLabel;
}
-(UITextField *)phoneNumberTF{
    if (!_phoneNumberTF) {
        _phoneNumberTF=[[UITextField alloc]initWithFrame:CGRectMake(52.5, CGRectGetMaxY(self.contentTitleLabel.frame)+15, KscreenWidth-101, 43)];
        _phoneNumberTF.borderStyle= UITextBorderStyleNone;
        _phoneNumberTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入手机号码" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:12], NSForegroundColorAttributeName:UIColorFromRGB(0x333333)}];
        _phoneNumberTF.textColor=UIColorFromRGB(0x333333);
        _phoneNumberTF.textAlignment = NSTextAlignmentCenter;
        _phoneNumberTF.font=[UIFont systemFontOfSize:12];
        _phoneNumberTF.keyboardType=UIKeyboardTypeDecimalPad;
        UIView * leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 97, 43)];
        _phoneNumberTF.leftView = leftView;
        CustomLabel * lab =[[CustomLabel alloc]initWithFrame:CGRectMake(0, 0, 91, 43) andText:@"中国大陆" andFont:13 andColor:UIColorFromRGB(0x333333)];
        lab.textAlignment = NSTextAlignmentCenter;
        
        [leftView addSubview:lab];
        
        CustomButton * btn =[[CustomButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab.frame), 15.5, 6, 10) andImageName:@"Rectangle" andTitle:@"" andColor:[UIColor whiteColor] andFont:15];
        [leftView addSubview:btn];
        _phoneNumberTF.leftViewMode = UITextFieldViewModeAlways;
        
    }
    return _phoneNumberTF;
}
-(UIView *)lineView{
    if (!_lineView) {
        _lineView=[[UIView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(self.phoneNumberTF.frame), KscreenWidth-100, 1)];
        _lineView.backgroundColor=UIColorFromRGB(0xDEDEDE);
    }
    return _lineView;
}
-(CustomButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn=[[CustomButton alloc]initWithFrame:CGRectMake(81.5, CGRectGetMaxY(self.lineView.frame)+36, KscreenWidth-163, 48)  andTitle:@"获取验证码" andColor:UIColorFromRGB(0xFFFFFF) andFont:18];
        _loginBtn.backgroundColor=UIColorFromRGB(0xFF5F57);
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
    
}
-(CustomLabel *)codeLabel{
    if (!_codeLabel) {
        _codeLabel=[[CustomLabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.contentTitleLabel.frame), CGRectGetMaxY(self.contentTitleLabel.frame)+10, 200, 15) andText:@"请输入短信验证码" andFont:11 andColor:UIColorFromRGB(0x666666)];
    }
    return _codeLabel;
}
- (CodeInputView *)codeView {
    //    __weak typeof(self) selfWeak = self;
    if (!_codeView) {
        _codeView = [[CodeInputView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 80) inputType:6 selectCodeBlock:^(NSString * code) {
      
        }];
       
    }
    return _codeView;
}
-(LoginViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel=[[LoginViewModel alloc]init];
    }
    return _viewModel;
}
@end
